package newx.com.control.scroll.text 
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class SystemText extends Sprite 
	{
		private static var _self		:SystemText;
		//所要显示的宽高度
		private var _showWidth			:Number		=	200;
		private var _showHeight		:Number		=	80;
		//聊天文本的宽度
		private var _width				:Number	=	200;
		private var _height			:Number	=	80;
		//单句话的显示数组
		private var _talkBoxArray		:Array;
		//单个聊天文本对象池
		private var _talkTextArray		:Array;
		//当前聊天显示对像该设定的高度
		private var _showTalkTextY		:Number	=	0;
		// 行间距
		private var _lineHeight		:int	=	0;
		//聊天显示对像容器
		private var _textSpace			:Sprite
		//最大的聊天内容条数
		private var _talkMaxLine		:int	=	30;
		//滚动条
		private var scrollBar			:NxNewScrollBar;
		//可视区域
		private var showRect			:Rectangle;
		//用与临时获取对像的显示大小
		private	var bitMap			:BitmapData
		private 	var bitSprit		:Sprite;
		//当前显示文本的真实高度
		private var _textSpaceHeight	:Number;
		//滚轮一次的参数
		private	var wheelData		:Number	=	12;
		//是否为锁屏状态
		private var _isLock			:Boolean		=	false;
		//链接返回函数
		private var textLinkFun		:Function;
		
		public function SystemText() 
		{
			init();
		}
		public static function getInstance():SystemText
		{
			if (_self == null) 
			{
				_self	=	new SystemText();
			}
			return _self;
		}
		/**
		 * 添加聊天内容
		 * @param	val 聊天内容
		 * @param	type 当前聊天内容的类型
		 */
		public function insterTalkStr(val:String):void
		{
			if (val == "") return;
			checkOutMaxLine()
			var messageText:NxTalkTextFiled	=	talkText();	
			if (!messageText.data.CheckProperties("onLinkHandle")) 
			{
				messageText.data.HandleProperties("onLinkHandle",textLinkFun);
			}
			messageText.textWidth			=	_width;
			messageText.x					=	0;
			messageText.y					=	_showTalkTextY;
			messageText.insterTalkStr(val);
			_showTalkTextY					+=	( messageText.height + _lineHeight );
			_textSpace.addChild(messageText);
			_textSpaceHeight				=	textHeight();
			_talkBoxArray.push(messageText);
			scrollBar.setHeight(showRect.height, _textSpaceHeight);
			this.scrollV					=	_textSpaceHeight - showRect.height;
			stopFaceIcon()
		}
		/**
		 * 添加私聊聊天内容
		 * @param	val 聊天内容
		 * @param	type 当前聊天内容的类型
		 */
		public function insterTalkStr1(val:String):void
		{
			if (val == "") return;
			checkOutMaxLine()
			var messageText:NxTalkTextFiled	=	talkText();	
			if (!messageText.data.CheckProperties("onLinkHandle")) 
			{
				messageText.data.HandleProperties("onLinkHandle",textLinkFun);
			}
			messageText.textWidth			=	_width;
			messageText.x					=	0;
			messageText.y					=	_showTalkTextY;
			messageText.insterTalkStr(val);
			_showTalkTextY					+=	( messageText.height + _lineHeight );
			_textSpace.addChild(messageText);
			_textSpaceHeight				=	textHeight();
			_talkBoxArray.push(messageText);
			scrollBar.setHeight(showRect.height, _showTalkTextY);
			if (_showTalkTextY < showRect.height) 
			{
				this.scrollV = 0
			}
			else
			{
				this.scrollV					=	_showTalkTextY - showRect.height;
			}
			stopFaceIcon()
			scrollmove();
		}
		/**
		 * 停止除当前聊天信息外的所有表情动作
		 */
		private function stopFaceIcon():void 
		{
			for (var i:int = 0; i < _talkBoxArray.length- 1; i++) 
			{
				var talkBox:NxTalkTextFiled	=	_talkBoxArray[i] as NxTalkTextFiled;
				if (!talkBox.isStopFactIcon)
				{
					talkBox.stopAllFaceIcon();
				}
			}
		}
		/**
		 * 当前聊天内容是否超出最大条数
		 */
		private function checkOutMaxLine():void 
		{
			if (_talkBoxArray.length > _talkMaxLine)
			{
				for (var i:int = 0; i < (_talkBoxArray.length - _talkMaxLine); i++) 
				{
					var removTalkBox:NxTalkTextFiled	=	_talkBoxArray.shift() as NxTalkTextFiled;
					returnTalkArrayText(removTalkBox);
				}
				_showTalkTextY	=	0;
				for (var j:int = 0; j < _talkBoxArray.length; j++) 
				{
					var talkBox:NxTalkTextFiled	=	_talkBoxArray[j] as NxTalkTextFiled;
					talkBox.x	=	0;
					talkBox.y	=	_showTalkTextY;
					_showTalkTextY	=	talkBox.y + talkBox.height + _lineHeight;
				}
			}
			else
			{
				return;
			}
			
		}
		/**
		 * 设定聊天文本的宽高度
		 * @param	width_ 
		 * @param	height_
		 */
		public function setSizeWH(width_:Number, height_:Number):void
		{
			_width	=	width_;
			_height	=	height_;
			_showWidth	=	_width;
			_showHeight	=	_height;
			reSetSize();
		}
		/**
		 * 清除聊天文本里的所有信息
		 */
		public function clearTalkText():void
		{
			for (var i:int = 0; i < _talkBoxArray.length; i++) 
			{
				returnTalkArrayText(_talkBoxArray[i]);
			}
			_talkBoxArray		=	[];
			_showTalkTextY		=	0
			showRect.y			=	0;
			_textSpaceHeight	=	0;
			scrollBar.setHeight(showRect.height, 0);
			scrollmove();
		}
		/**
		 * 添加超链接事件
		 * @param	val
		 */
		public function addLinkClickHandel(val:Function):void
		{
			textLinkFun	=	val;
			for (var i:int = 0; i < _talkBoxArray.length; i++) 
			{
				var textBox:NxTalkTextFiled	=	_talkBoxArray[i] as NxTalkTextFiled;
				if (!textBox.data.CheckProperties("onLinkHandle")) 
				{
					textBox.data.HandleProperties("onLinkHandle",textLinkFun);
				}
			}
		}
		/**
		 * 设定文滚动值
		 */
		public function set scrollV(val:Number):void
		{
			if (!_isLock && val != showRect.y) 
			{
				showRect.y	=	val;
				scrollmove();
			}
			if (val != showRect.y) 
			{
				if (!_isLock) 
				{
					showRect.y	=	val;
					scrollmove();
				}
				else
				{
					if (val < 0) 
					{
						showRect.y	=	val;
						scrollmove();
					}
				}
			}
		}
		/**
		 * 获取当前最大的可滚动坐标
		 */
		public function get MaxScrollV():Number
		{
			return _textSpaceHeight - showRect.height;
		}
		/**
		 * 设定是否锁屏
		 */
		public function set isLock(value:Boolean):void 
		{
			_isLock = value;
		}
		public function get isLock():Boolean
		{
			return _isLock;
		}
		/**
		 * 所要显示的宽度
		 */
		public function get showWidth():Number 
		{
			return _showWidth;
		}
		/**
		 * 所要显示的高度
		 */
		public function get showHeight():Number 
		{
			return _showHeight;
		}
		/**
		 * 初始化
		 */
		private function init():void
		{
			_talkTextArray	=	[];
			_talkBoxArray	=	[];
			
			bitSprit		=	new Sprite();
			_textSpace		=	new Sprite();
			scrollBar		=	new NxNewScrollBar();
			bitMap			=	new BitmapData(1, 1);
			showRect		=	new Rectangle(0,0,_width,_height);
			scrollBar.bind(scrollChange)
			this.addChild(_textSpace);
			this.addChild(scrollBar);
			_textSpace.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandle)
			reSetSize();
		}
		/**
		 * 文本滚轮事件
		 * @param	e
		 */
		private function mouseWheelHandle(e:MouseEvent):void 
		{
			if (_textSpaceHeight < showRect.height) 
			{
				return;
			}
			var tempY:int	=	showRect.y;
			tempY	-= e.delta * wheelData;
			if(tempY < 0)
			{
				tempY	=	0;
			}
			else if(tempY >= (_textSpaceHeight - _height))
			{
				tempY	=	_textSpaceHeight - _height;
			}
			if(tempY != showRect.y)
			{
				showRect.y	=	tempY;
				scrollmove();
			}
		}
		/**
		 * 改变滚动条的滑块坐标
		 */
		private function scrollmove():void 
		{
			scrollBar.Ratio	=	showRect.y;
		}
		/**
		 * 刷新显示区域
		 */
		private function reSetSize():void 
		{
			showRect.width			=	_width;
			showRect.height			=	_height;
			if (_isLock) 
			{
				if (showRect.y <0 || _textSpaceHeight - showRect.y < showRect.height) 
				{
					showRect.y	=	_textSpaceHeight - showRect.height;
					scrollmove();
				}
				
			}
			else
			{
				this.scrollV			=	_textSpaceHeight - showRect.height;
			}
			scrollBar.x					=	- 14;
			scrollBar.setHeight(showRect.height, _textSpaceHeight);
		}
		/**
		 * 获取显示对像的高度
		 */
		private function textHeight():Number
		{
			var tempHeight:Number;
			_textSpace.scrollRect	=	null;
			bitMap.draw(bitSprit);
			tempHeight				=	_textSpace.height;
			_textSpace.scrollRect	=	showRect;
			return tempHeight;
		}
		/**
		 * 滚动条回调函数
		 */
		private function scrollChange():void 
		{
			showRect.y				=	scrollBar.Ratio;
			_textSpace.scrollRect	=	showRect;
		}
		/**
		 * 生成聊天单个显示对像
		 * @return
		 */
		private function talkText():NxTalkTextFiled
		{
			var returnText:NxTalkTextFiled	=	getTalkArrayText();
			if (returnText == null) 
			{
				returnText	=	new NxTalkTextFiled(_width);
			}
			return returnText;
		}
		/**
		 * 从对像池中获取聊天文本
		 * @return
		 */
		private function getTalkArrayText():NxTalkTextFiled
		{
			var returnText:NxTalkTextFiled	=	null
			if (_talkTextArray.length != 0) 
			{
				returnText	=	_talkTextArray.shift() as NxTalkTextFiled;
			}
			return returnText;
		}
		/**
		 * 归还聊天文本
		 * @param	val
		 */
		private function returnTalkArrayText(val:NxTalkTextFiled):void
		{
			val.clearAll();
			_talkTextArray.push(val);
			if (val.parent != null) 
			{
				val.parent.removeChild(val);
			}
		}
		
		public function hideScrollBar():void
		{
			scrollBar.alpha = 0;
		}
		
		public function showScrollBar():void
		{
			scrollBar.alpha = 1;
		}
		
	}
	
}